package com.hqs.listener.activity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hqs.common.utils.ActivityUtil;
import com.hqs.common.utils.ScreenUtils;
import com.hqs.common.utils.StatusBarUtil;
import com.hqs.common.view.QSViewPager;
import com.hqs.listener.R;
import com.hqs.listener.fragment.FileListFragment;
import com.hqs.listener.fragment.PlayerFragment;
import com.hqs.listener.fragment.TimerFragment;
import com.hqs.listener.helper.Helper;
import com.hqs.listener.service.MusicService;
import com.hqs.listener.view.TabBarView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity {


    @BindView(R.id.tabBarView)
    public TabBarView tabBarView;

    @BindView(R.id.title)
    public TextView titleView;

    @BindView(R.id.viewPager)
    public QSViewPager viewPager;

    private QAdapter adapter;

    public String[] titles = {"文件列表", "播放器", "倒计时"};

    public ServiceCon sc;

    public static MainActivity instance;
    public MusicService service;

    RelativeLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StatusBarUtil.transparencyBar(this);
        rootView = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
        setContentView(rootView);

        MainActivity.instance = this;
        ButterKnife.bind(this);
        ActivityUtil.hideActionBar(this);

        PackageManager manager = getPackageManager();
        boolean permission = (PackageManager.PERMISSION_GRANTED ==
                manager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getPackageName()));
        if (!permission) {
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO
            }, 999);
        }


        tabBarView.setOnClickTabBarListener(new TabBarView.OnClickTabBarListener() {
            @Override
            public void onClick(int position) {
                viewPager.setCurrentItem(position, false);
                if (position == 0){
                    titleView.setEnabled(true);
                }
                else {
                    titleView.setEnabled(false);
                }
                if (position == 2){
                    titleView.setText(titles[position]);
                }

            }
        });

        viewPager.setHorizontalScrollable(false);

        adapter = new QAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        tabBarView.select(1);
        sc = new ServiceCon();
        bindService();

        welcomeView();
    }

    public RelativeLayout contentView() {
        final RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setBackgroundResource(R.color.startBgColor);
        relativeLayout.setGravity(Gravity.CENTER);
        ImageView logo = new ImageView(this);
        logo.setImageResource(R.mipmap.logo);
        int wh = (int) (ScreenUtils.screenW(this) * 0.99);
        logo.setLayoutParams(new RelativeLayout.LayoutParams(wh, wh));
        logo.setScaleType(ImageView.ScaleType.FIT_XY);
        relativeLayout.addView(logo);

        return relativeLayout;
    }

    void bindService(){
        Intent intent = new Intent(this, MusicService.class);
        intent.setAction("music.service");
        bindService(intent, sc, Context.BIND_AUTO_CREATE);

        intent = new Intent(this, MusicService.class);
        startService(intent);
    }

    void welcomeView(){
        final RelativeLayout relativeLayout = contentView();

        relativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        rootView.addView(relativeLayout);

        Helper.doInMainThreadDelayed(new Runnable() {
            @Override
            public void run() {
                Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_out_left);
                relativeLayout.startAnimation(anim);
                anim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        rootView.removeView(relativeLayout);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
        }, 300);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(sc);
    }

    class ServiceCon implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            service = ((MusicService.LocalBinder) iBinder).getService();
        }

        @Override
        public void onBindingDied(ComponentName name) {
//            Log.print(name);
//            Log.print("onBindingDied");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
//            Log.print("onServiceDisconnected");
        }
    }

    class QAdapter extends FragmentPagerAdapter {



        private ArrayList<Fragment> fragments;

        public QAdapter(FragmentManager fm) {
            super(fm);

            FileListFragment fileListFragment = new FileListFragment();
            PlayerFragment playerFragment = new PlayerFragment();
            TimerFragment timerFragment = new TimerFragment();
            fragments = new ArrayList<>();
            fragments.add(fileListFragment);
            fragments.add(playerFragment);
            fragments.add(timerFragment);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    }
}

